package frame;

import dataediter.MyLogWR;
import dataediter.TableWR;
import main.Manager;
import main.Resource;
import other.Tool;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

/**
 * 索引页面
 *
 * @author fdh
 */
public class IndexFrame extends FrameBase {
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JTable leftPanelTable;
    private JScrollPane leftPanelTableScrollPane;
    private JScrollPane leftFooterScrollPane;
    private JLabel leftTopLabel;
    private JLabel leftFooterLabel;
    private JLabel leftTableShowColAndRowLabel;
    private JButton createTableBtn;
    private JButton checkTableBtn;
    private JButton saveTableButton;
    private JButton delTableButton;
    private JButton searchTableDataButton;
    private MyLogWR logEditor = new MyLogWR();

    public IndexFrame() {
        super();
        frame.setTitle(Resource.IndexFrameResource.title);
        frame.setSize(Resource.IndexFrameResource.frameSize);
        leftPanel = iniLeftPanel();
        rightPanel = iniRightPanel();
        frame.getLayeredPane().add(leftPanel, 1);
        frame.getLayeredPane().add(rightPanel, 1);
        setLeftPanel();
        setRightPanel();
        frame.setVisible(true);
        this.iniFrameListener();
    }

    private JPanel iniLeftPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setLocation(Resource.IndexFrameResource.leftPanelPos);
        panel.setSize((int) (frame.getWidth() * Resource.IndexFrameResource.leftRatio), frame.getHeight());
        Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
        panel.setBorder(border);
        return panel;
    }

    private JPanel iniRightPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setLocation((int) (frame.getWidth() * Resource.IndexFrameResource.leftRatio), 0);
        panel.setSize((int) (frame.getWidth() * Resource.IndexFrameResource.rightRatio), frame.getHeight());
        Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
        panel.setBorder(border);
        return panel;
    }


    private void setLeftPanel() {
        leftPanelTable = new JTable();
        leftTableShowColAndRowLabel = Tool.getInstance().getMSYaHeiJLabel("col:0 row:0", 10);
        saveTableButton = new JButton("保存表格");
        leftTopLabel = Tool.getInstance().getSongTiJLabel("表名:none", 10);
        leftFooterLabel = Tool.getInstance().getSongTiJLabel(Resource.IndexFrameResource.footerLabelRemindTxt, 10);
        leftPanelTableScrollPane = new JScrollPane(leftPanelTable);
        leftFooterScrollPane = new JScrollPane(leftFooterLabel);
        leftPanel.add(leftPanelTableScrollPane);
        leftPanel.add(leftTableShowColAndRowLabel);
        leftPanel.add(leftTopLabel);
        leftPanel.add(saveTableButton);
        leftPanel.add(leftFooterScrollPane);
        saveTableButton.addActionListener(e -> {
            if (Manager.getInstance().currentTableFile.equals("-1"))
                return;
            saveTable(true, Manager.getInstance().currentTableFile);
        });
        changeLeftPanel();
    }

    private void setRightPanel() {
        createTableBtn = new JButton(Resource.IndexFrameResource.createTableTxt);
        checkTableBtn = new JButton(Resource.IndexFrameResource.checkTableTxt);
        delTableButton = new JButton(Resource.IndexFrameResource.delTableTxt);
        searchTableDataButton = new JButton(Resource.IndexFrameResource.searchTableDataTxt);
        rightPanel.add(createTableBtn);
        rightPanel.add(checkTableBtn);
        rightPanel.add(delTableButton);
        rightPanel.add(searchTableDataButton);
        createTableBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setCreateTableBtn();
            }
        });
        checkTableBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setCheckTableBtn();
            }
        });
        delTableButton.addActionListener(e ->
        {
            setDelButton();
        });
        searchTableDataButton.addActionListener(e ->
        {
            setSearchTableDataButton();
        });
    }

    private void changeLeftPanel() {
        int width = (int) (leftPanel.getWidth() * 0.8);
        int height = (int) (leftPanel.getHeight() * 0.8);
        int leftTopHeight = (int) (leftPanel.getHeight() * 0.05);
        int leftPos = (int) (leftPanel.getWidth() * 0.1);
        leftTopLabel.setBounds(leftPos, (int) (leftPanel.getHeight() * 0.05) - leftTopHeight, 150, leftTopHeight);
        saveTableButton.setBounds(leftPos + leftPanelTableScrollPane.getWidth() - 100,
                (int) (leftPanel.getHeight() * 0.05) - leftTopHeight + 5, 100, leftTopHeight - 10);
        leftTableShowColAndRowLabel.setBounds(leftPos + leftPanelTableScrollPane.getWidth() - 100 - 150,
                (int) (leftPanel.getHeight() * 0.05) - leftTopHeight + 5, 150, leftTopHeight - 10);
        leftPanelTableScrollPane.setBounds(leftPos, (int) (leftPanel.getHeight() * 0.05), width, height);
        leftFooterScrollPane.setSize(width, (int) (leftPanel.getHeight() * 0.05));
        leftFooterScrollPane.setLocation(leftPos, (int) (leftPanel.getHeight() * 0.85));
    }

    private void changeRightPanel() {
        int width = (int) (rightPanel.getWidth() * 0.6);
        int height = 30;
        Point startPos = new Point((int) (rightPanel.getWidth() * 0.2), 40);
        createTableBtn.setSize(width, height);
        createTableBtn.setLocation(startPos);
        checkTableBtn.setBounds(startPos.x, startPos.y + height + 10, width, height);
        delTableButton.setBounds(startPos.x, startPos.y + height * 2 + 10 * 2, width, height);
        searchTableDataButton.setBounds(startPos.x, startPos.y + height * 3 + 10 * 3, width, height);
    }

    private void setCreateTableBtn() {
        JDialog dialog = new JDialog(frame, Resource.IndexFrameResource.createTableTxt, true);
        dialog.setSize(Resource.IndexFrameResource.createTableDialogSize);
        dialog.setLayout(null);
        JLabel tableName = Tool.getInstance().getMSYaHeiJLabel("表名", 15);
        JLabel tableColNum = Tool.getInstance().getMSYaHeiJLabel("列数", 15);
        JTextField tableNameTextField = new JTextField();
        JTextField tableColNumTextField = new JTextField();
        JButton rightBtn = new JButton("确定");
        dialog.add(tableName);
        dialog.add(tableNameTextField);
        dialog.add(tableColNum);
        dialog.add(tableColNumTextField);
        dialog.add(rightBtn);
        int width = 50;
        int height = 20;
        tableName.setBounds(10, 10, width, height);
        tableNameTextField.setBounds(15 + width, 10, width * 3, height);
        tableColNum.setBounds(10, 10 + height + 5, width, height);
        tableColNumTextField.setBounds(15 + width, 10 + height + 5, width * 3, height);
        rightBtn.setBounds(10 + width, 10 + height * 2 + 5 * 2, width * 2, height);
        rightBtn.addActionListener(e -> {
            int colNum;
            try {
                colNum = Integer.parseInt(tableColNumTextField.getText());
            } catch (Exception ex) {
                System.out.println("列数输入错误,不是数字");
                JOptionPane.showConfirmDialog(dialog, "列数输入错误,列数不是数字", "创建失败", JOptionPane.DEFAULT_OPTION);
                return;
            }
            if (new TableWR().createFile(tableNameTextField.getText())) {
                Manager.getInstance().allTableFile.add(tableNameTextField.getText() + "." + Resource.Tables.TablesMakers.suffixName);
                Manager.getInstance().allTableData.put(tableNameTextField.getText() + "." + Resource.Tables.TablesMakers.suffixName,
                        Tool.getInstance().setArrayEmpty(new String[Resource.Config.tableMaxRowCount][colNum]));
                Manager.getInstance().allTableColName.put(tableNameTextField.getText() + "." + Resource.Tables.TablesMakers.suffixName,
                        Tool.getInstance().setArrayEmpty(new String[colNum]));
                dialog.dispose();
            } else {
                JOptionPane.showConfirmDialog(frame, "创建失败,文件名重复", "创建失败", JOptionPane.DEFAULT_OPTION);
            }
        });
        dialog.setVisible(true);
    }

    private void setCheckTableBtn() {
        JDialog dialog = new JDialog(frame, Resource.IndexFrameResource.checkTableTxt, true);
        dialog.setSize(Resource.IndexFrameResource.checkTableDialogSize);
        dialog.setLayout(null);
        JButton sureButton = new JButton("确定");
        ArrayList<String> list = getAllTableName(true);
        JComboBox<String> tableNameComboBox = new JComboBox<>(list.toArray(new String[0]));
        int width = (int) (dialog.getWidth() * 0.8);
        int height = 30;
        tableNameComboBox.setBounds(10, 10, width, height);
        sureButton.setBounds(10, 10 + height + 5, width, height);
        tableNameComboBox.setSelectedIndex(0);
        dialog.add(tableNameComboBox);
        dialog.add(sureButton);
        sureButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!tableNameComboBox.getSelectedItem().toString().equals("none")) {
                    Manager.getInstance().setCurrentTableFile(tableNameComboBox.getSelectedItem().toString() + "."
                            + Resource.Tables.TablesMakers.suffixName);
                    //表格切换
                    changeTableShow(Manager.getInstance().allTableData.get(Manager.getInstance().currentTableFile),
                            Manager.getInstance().allTableColName.get(Manager.getInstance().currentTableFile));
                }
                dialog.dispose();
            }
        });
        dialog.setVisible(true);
    }

    private void setDelButton() {
        JDialog dialog = new JDialog(frame, Resource.IndexFrameResource.delTableTxt, true);
        dialog.setSize(Resource.IndexFrameResource.delTableDialogSize);
        dialog.setLayout(null);
        ArrayList<String> tableNameList = getAllTableName(true);
        JComboBox<String> selects = new JComboBox<>(tableNameList.toArray(new String[0]));
        JButton sureBtn = new JButton("确定");
        if (Manager.getInstance().currentTableFile.equals("-1")) {
            selects.setSelectedIndex(0);
        } else {
            int index = 0;
            String tableName = Manager.getInstance().currentTableFile.split("\\.")[0];
            for (String s : tableNameList) {
                if (s.equals(tableName)) {
                    break;
                }
                index++;
            }
            selects.setSelectedIndex(index);
        }
        dialog.add(selects);
        dialog.add(sureBtn);
        int width = (int) (dialog.getWidth() * 0.8);
        int height = 30;
        selects.setBounds(10, 10, width, height);
        sureBtn.setBounds(10, 10 + height + 10, width, height);
        sureBtn.addActionListener(e ->
        {
            String tableName = selects.getSelectedItem().toString();
            if (!tableName.equals("none")) {
                String file = tableName + "." + Resource.Tables.TablesMakers.suffixName;
                Manager.getInstance().allTableFile.remove(file);
                if (Manager.getInstance().currentTableFile.equals(file)) {
                    Manager.getInstance().currentTableFile = "-1";
                    changeTableShow(null, null);
                }
                new TableWR().delFileByName(tableName);
                new MyLogWR().delFileByName(tableName);
                System.out.println("日志文件和表格文件删除成功:" + tableName);
            }
            dialog.dispose();
        });
        dialog.setVisible(true);
    }

    private void setSearchTableDataButton() {
        JDialog dialog = new JDialog(frame, Resource.IndexFrameResource.searchTableDataTxt, true);
        dialog.setSize(Resource.IndexFrameResource.searchTableDataDialogSize);
        dialog.setLayout(null);
        ArrayList<String> allTableName = getAllTableName(true);
        JComboBox<String> selects = new JComboBox<>(allTableName.toArray(new String[0]));
        JTextField colInput = new JTextField();
        JTextField dataInput = new JTextField();
        JButton sureBtn = new JButton("确定");
        JLabel tableNameTxt = Tool.getInstance().getMSYaHeiJLabel("表名:", 15);
        JLabel colNameTxt = Tool.getInstance().getMSYaHeiJLabel("列名:", 15);
        JLabel dataNameTxt = Tool.getInstance().getMSYaHeiJLabel("数据:", 15);
        JLabel remindTxt = Tool.getInstance().getSongTiJLabel(Resource.IndexFrameResource.searchDataDialogRemind, 10);
        dialog.add(selects);
        dialog.add(colNameTxt);
        dialog.add(dataNameTxt);
        dialog.add(tableNameTxt);
        dialog.add(colInput);
        dialog.add(dataInput);
        dialog.add(remindTxt);
        dialog.add(sureBtn);
        tableNameTxt.setBounds(10, 10, 50, 30);
        selects.setBounds(70, 10, 250, 30);
        colNameTxt.setBounds(10, 50, 50, 30);
        colInput.setBounds(70, 50, 250, 30);
        dataNameTxt.setBounds(10, 90, 50, 30);
        dataInput.setBounds(70, 90, 250, 30);
        remindTxt.setBounds(10, 130, 300, 30);
        sureBtn.setBounds(10, 170, 100, 30);
        sureBtn.addActionListener(e -> {
            String tableName = selects.getSelectedItem().toString();
            if (tableName.equals("none")) {
                dialog.dispose();
                return;
            }
            String colName = colInput.getText();
            String data = dataInput.getText();
            tableName += "." + Resource.Tables.TablesMakers.suffixName;
            String[] cols = Manager.getInstance().allTableColName.get(tableName);
            String[][] datas = Manager.getInstance().allTableData.get(tableName);
            String[] inputCols = colName.split(",");
            int[] inputColIndex = new int[inputCols.length];
            String[] inputDatas = data.split(",");
            if (inputDatas.length != inputCols.length || (inputCols[0].isEmpty() || inputDatas[0].isEmpty())) {
                JOptionPane.showConfirmDialog(dialog, "确保查询数据和表名一一对应,且非空", "查询错误", JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE);
                return;
            }
            for (int i = 0; i < inputCols.length; i++) {
                inputColIndex[i] = -1;
                for (int j = 0; j < cols.length; j++) {
                    if (inputCols[i].equals(cols[j])) {
                        inputColIndex[i] = j;
                    }
                }
                if (inputColIndex[i] == -1) {
                    JOptionPane.showConfirmDialog(dialog, "无此列名", "查询错误", JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE);
                    return;
                }
            }
            ArrayList<String[]> re = new ArrayList<>();
            for (int i = 0; i < datas.length; i++) {
                boolean isFlag = true;
                for (int j = 0; j < inputColIndex.length; j++) {
                    System.out.println(datas[i][inputColIndex[j]] + " " + inputDatas[j]);
                    if (!datas[i][inputColIndex[j]].trim().equals(inputDatas[j].trim())) {
                        isFlag = false;
                        break;
                    }
                }
                if (isFlag) {
                    re.add(datas[i]);
                }
            }
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < cols.length; i++) {
                result.append(String.format("%-" + Resource.IndexFrameResource.searchTableDataDialogResultMaxLength + "s", cols[i]));
            }
            result.append("\n");
            for (int i = 0; i < re.size(); i++) {
                for (int j = 0; j < re.get(i).length; j++) {
                    result.append(String.format("%-" + Resource.IndexFrameResource.searchTableDataDialogResultMaxLength + "s", re.get(i)[j]));
                }
                result.append("\n");
            }
            JOptionPane.showConfirmDialog(frame, result.toString(), "查询结果", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
        });
        dialog.setVisible(true);
    }

    /**
     * 修改图表
     *
     * @param datas  数据
     * @param column 列名
     */
    public void changeTableShow(String[][] datas, String[] column) {
        if (!Manager.getInstance().preTableFile.equals("-1")) {
            int choose = JOptionPane.YES_OPTION;
            if (!Resource.Config.enableAutoSave) {
                System.out.println("自动保存开启");
                choose = JOptionPane.showConfirmDialog(frame, "是否保存表格?", "是否保存", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            }
            if (choose == JOptionPane.YES_OPTION)
                saveTable(false, Manager.getInstance().preTableFile);
            /*
            偷懒懒得优化了，暴力解决，因为设计初期的时候没有考虑这个功能,导致在修改表格时是直接修改源数据,所以不管选择是和否,源数据都已经被修改
            解决的方法有两个,其中一个就是下面这段简单省力重新从表格文件里获取源数据
            另一个方法就是不直接修改源数据,而是修改源数据的副本,修改后再判断是否覆盖源数据,实现也不难,毕竟源数据就是数组嘛直接clone()一个副本就行,
            但是,懒嘛,懒得写了 ^-^!!!!!!                  --Date 2024.5.8 18:43 fdh(懒癌晚期患者)
            */
            if (choose == JOptionPane.NO_OPTION)
                Manager.getInstance().getTablesFromDir();
        }
        changeTableData(datas, column);
        if (Manager.getInstance().currentTableFile.equals("-1")) {
            leftTopLabel.setText("表名:none");
            System.out.println("-1,无法创建图表");
        } else {
            leftTopLabel.setText("表名:" + Manager.getInstance().currentTableFile.split("\\.")[0]);
            System.out.println(Manager.getInstance().currentTableFile + "图表修改完成");
        }
    }

    private void changeTableData(String[][] data, String[] col) {
        DefaultTableModel model = new DefaultTableModel(data, col);
        leftPanelTable.setModel(model);
        addTableListener();
    }

    private void addTableListener() {
        leftPanelTable.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                int getCol = e.getColumn();
                int getFirstRow = e.getFirstRow();
//                int getLastRow = e.getLastRow();
                setColAndRowShowLabel(getCol, getFirstRow);
                String preData = Manager.getInstance().allTableData
                        .get(Manager.getInstance().currentTableFile)[getFirstRow][getCol];
                String nowData = leftPanelTable.getModel().getValueAt(getFirstRow, getCol).toString();
                //添加更改数据的log信息
                if (!preData.equals(nowData)) {
                    String pre = preData.isEmpty() ? "null" : preData;
                    String current = nowData.isEmpty() ? "null" : nowData;
                    leftFooterLabel.setText(leftFooterLabel.getText() + " /表格数据变化:" + " 列:" + getCol + " 行:" + getFirstRow
                            + " 数据变化:" + pre + " to " + current);
                    logEditor.addLogInfo(" 列:" + getCol + " 行:" + getFirstRow + " 数据变化:" + pre + " to " + current);
                }
                Manager.getInstance().allTableData.get(Manager.getInstance().currentTableFile)[getFirstRow][getCol] = nowData;
            }
        });
    }

    /**
     * 保存表格
     */
    private void saveTable(boolean isChangeTable, String preOrCurrent) {
        if (preOrCurrent.equals("-1"))
            return;
        System.out.println("开始保存表格:" + preOrCurrent);
        try {
            String[][] allData = Manager.getInstance().allTableData.get(preOrCurrent);
            String[] allColName = Manager.getInstance().allTableColName.get(preOrCurrent);
            //判断是否有列名，如果没有则默认第一行为列名,并且列名的长度就是连续数据的长度
            if (allData[0][0] != null && !allData[0][0].isEmpty()) {
                if ((allColName[0] == null || allColName[0].isEmpty())) {
                    int colNum = allData[0].length;
                    for (int i = 0; i < colNum; i++) {
                        if (allData[0][i] == null || allData[0][i].isEmpty()) {
                            colNum = i;
                            break;
                        }
                    }
                    String[][] newDatas = new String[Resource.Config.tableMaxRowCount][colNum];
                    String[] newCol = new String[colNum];
                    for (int i = 0; i < allData.length; i++) {
                        for (int j = 0; j < colNum; j++) {
                            if (i == 0) {
                                newCol[j] = allData[i][j];
                            } else {
                                newDatas[i - 1][j] = allData[i][j];
                            }
                        }
                    }
                    allData = newDatas;
                    allColName = newCol;
                    logEditor.addLogInfo(Resource.MyLog.Maker.remindLineMaker + "第一行转换为列名");
                }
            }
            //判断是否有空行，如果有则删除
            int startIndex = -1;
            if (Resource.Config.enableOverwriteEmptyRows) {
                for (int i = 0; i < allData.length; i++) {
                    boolean flag = true;
                    for (int j = 0; j < allColName.length; j++) {
                        if (allData[i][j] != null && !allData[i][j].isEmpty()) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        startIndex = i;
                        break;
                    }
                }
                ArrayList<Integer> notNullLine = new ArrayList<>();
                for (int j = startIndex; j < allData.length; j++) {
                    boolean flag = true;
                    for (int k = 0; k < allColName.length; k++) {
                        if (allData[j][k] != null && !allData[j][k].isEmpty()) {
                            flag = false;
                            break;
                        }
                    }
                    if (!flag) {
                        notNullLine.add(j);
                    }
                }
                int index = 0;
                for (int i = startIndex; i < allData.length; i++) {
                    if (index < notNullLine.size()) {
                        allData[i] = allData[notNullLine.get(index)].clone();
                        logEditor.addLogInfo(Resource.MyLog.Maker.remindLineMaker + "表内空行覆盖开启,移动行:" + notNullLine.get(index) + " to " + i);
                        index++;
                    } else {
                        for (int j = 0; j < allColName.length; j++) {
                            allData[i][j] = "";
                        }
                    }
                }
            }
            if (isChangeTable)
                changeTableData(allData, allColName);
            new TableWR(Resource.Tables.tablesPath + preOrCurrent)
                    .writeToTableFile(allData, allColName);
            Manager.getInstance().allTableData.put(preOrCurrent, allData);
            Manager.getInstance().allTableColName.put(preOrCurrent, allColName);
            writeLogToFile(preOrCurrent);
        } catch (Exception ex) {
            System.out.println("保存表格失败:" + preOrCurrent);
            System.out.println(ex.getMessage());
        }
    }

    /**
     * 改变坐标显示
     *
     * @param col 列
     * @param row 行
     */
    private void setColAndRowShowLabel(int col, int row) {
        leftTableShowColAndRowLabel.setText("col:" + col + " " + "row:" + row);
    }

    private void changePanelSize(JPanel panel, double ratio, Point pos) {
        panel.setLocation(pos);
        panel.setSize((int) (frame.getWidth() * ratio), frame.getHeight());
    }

    private ArrayList<String> getAllTableName(boolean isAddNone) {
        ArrayList<String> list = new ArrayList<>();
        if (isAddNone)
            list.add("none");
        for (String s : Manager.getInstance().allTableFile) {
            list.add(s.split("\\.")[0]);
        }
        return list;
    }

    /**
     * 把log信息写入log文件
     *
     * @param tableFile
     */
    private void writeLogToFile(String tableFile) {
        if (!tableFile.equals("-1")) {
            try {
                logEditor.setFileName(tableFile.split("\\.")[0]);
                logEditor.createFile();
            } catch (Exception ex) {
                System.out.println("日志文件创建失败,无法保存数据,请检查后缀名");
                return;
            }
            //填写log信息
            logEditor.writeLogsToFile();
        }
    }


    @Override
    protected void selfAdaption() {
        changePanelSize(leftPanel, Resource.IndexFrameResource.leftRatio, new Point(0, 0));
        changePanelSize(rightPanel, Resource.IndexFrameResource.rightRatio, new Point(leftPanel.getWidth(), 0));
        changeLeftPanel();
        changeRightPanel();
    }

    @Override
    protected void iniFrameListener() {
        super.iniFrameListener();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (!Manager.getInstance().currentTableFile.equals("-1")) {
                    System.out.println("开始最后的保存:" + Manager.getInstance().currentTableFile);
                    int choose = JOptionPane.YES_OPTION;
                    if (!Resource.Config.enableAutoSave)
                        choose = JOptionPane.showConfirmDialog(frame, "是否保存表格?", "是否保存", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                    if (choose == JOptionPane.YES_OPTION)
                        saveTable(false, Manager.getInstance().currentTableFile);
                }
                System.out.println("程序结束啦,马上开香槟!!!!!!!");
            }
        });
    }
}